//
//  WordTravelVC.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/4.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit
import BTNavigationDropdownMenu

class WordTravelVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    let width = UIScreen.mainScreen().bounds.width
    let height = UIScreen.mainScreen().bounds.height
    
    var layout:SHHMyLayout!
    var collectionView:UICollectionView!
    
    var dataArr = Array<WordTravelModel>()
    
    //url
    let word_urlString = "http://www.koubeilvxing.com/countrys"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
//        self.dropDownList()
//        self.drawCollectoinView()
        self.getData()
    }
    
    func getData() {
        SHHGetData.sharedGetData.requestAllModelWithUrlString(word_urlString) { (array:Array<Array<WordTravelModel>>) -> Void in
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.dataArr = array[0]
                self.dropDownList()
                self.drawCollectoinView(self.dataArr.count)
                self.collectionView.reloadData()
            })
        }
    }
    
    //下拉菜单的布局
    func dropDownList() {
        self.view.backgroundColor = UIColor.greenColor()
        self.navigationItem.title = "世界探索"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "HTHome_Menu"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showSideView"))
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0)
        let items = ["亚洲","欧洲","美洲","南美洲","非洲","大洋洲"]
        let menuView = BTNavigationDropdownMenu(navigationController: navigationController, title: items.first!, items: items)
        menuView.cellHeight = 50
        menuView.cellBackgroundColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0)
        menuView.cellTextLabelColor = UIColor.blackColor()
        //        menuView.cellSeparatorColor = UIColor.blackColor()
        menuView.cellSelectionColor = UIColor(red: 0.0/255.0, green:180/255.0, blue:220/255.0, alpha: 1.0)
        menuView.cellTextLabelAlignment = .Left
        menuView.arrowPadding = 15
        menuView.animationDuration = 0.5
        menuView.maskBackgroundColor = UIColor.blackColor()
        menuView.maskBackgroundOpacity = 0.3
        menuView.didSelectItemAtIndexHandler = {(indexPath: Int) -> () in
            print("Did select item at index: \(indexPath)")
            self.dataArr.removeAll()
            self.dataArr = SHHGetData.sharedGetData.arrayWithArray[indexPath]
            print(SHHGetData.sharedGetData.arrayWithArray[indexPath].count)
            self.drawCollectoinView(SHHGetData.sharedGetData.arrayWithArray[indexPath].count)
        }
        
        self.navigationItem.titleView = menuView
    }
    
    //点击左侧按钮跳转页面
    func showSideView()->Void {
        
        self.sideMenuViewController.presentLeftMenuViewController()
    }
    
    //瀑布流的布局
    func drawCollectoinView(count:Int) {
        layout = SHHMyLayout()
        layout.scrollDirection = UICollectionViewScrollDirection.Vertical
        layout.itemCount = count
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 2, 2)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView = UICollectionView(frame: CGRectMake(0, 65, width, height), collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.whiteColor()
//        collectionView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cell")
        collectionView.registerNib(UINib(nibName: "WordTravelCVCell", bundle: nil), forCellWithReuseIdentifier: "Cell")
        self.view.addSubview(collectionView)

    }
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as? WordTravelCVCell
        
        cell?.picView.sd_setImageWithURL(NSURL(string: self.dataArr[indexPath.row].cover!), placeholderImage: nil)
        cell?.nameLabel.text = self.dataArr[indexPath.row].name_cn!
        
        return cell!
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("1111111")
        let cityTVC = CityTableViewController()
        let navigation = UINavigationController(rootViewController: cityTVC)
        let model = self.dataArr[indexPath.row]
        cityTVC.ID = model.ID!
        cityTVC.titleName = model.name_cn!
        print("========\(model.ID!)=====\(model.name_cn!)")
        self.presentViewController(navigation, animated: true, completion: nil)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
